package com.company.huisuo.biz.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import com.company.huisuo.base.BaseSingleAdapter
import com.company.huisuo.biz.bean.CategoryBean
import com.company.huisuo.databinding.ItemCartTypeBinding


class CartTypeAdapter(private val onCategoryClick: ((CategoryBean) -> Unit)? = null) : BaseSingleAdapter<CategoryBean, ItemCartTypeBinding>() {

    private var mSelectPos = 0





    override val initViewBinding: (LayoutInflater, ViewGroup, Boolean) -> ItemCartTypeBinding
        get() = ItemCartTypeBinding::inflate

    override fun convert(
        holder: SingleViewHolder<ItemCartTypeBinding>, position: Int, item: CategoryBean?
    ) {
        holder.binding.ivStar.isVisible = position == 0
        holder.binding.tvContent.text = item?.name ?: ""
        holder.binding.clLayout.isSelected = position == mSelectPos
        holder.binding.tvContent.isSelected = position == mSelectPos
        holder.binding.clLayout.setOnClickListener {
            mSelectPos = position
            notifyDataSetChanged()
            // 触发分类点击回调
            item?.let { category ->
                onCategoryClick?.invoke(category)
            }
        }
    }
}